# 1. Steepness
setup.steepness <- function(rungrp,newsteep,add_header=F,gap=0){
  rungrp$doitall <- steepness.doit(rungrp$doitall,newsteep,add_header,gap)
  return(rungrp)
}

# 1b. steepadd
setup.steep_add <- function(rungrp,steep_add){
  a <- rungrp$doitall
  pos <- grep("recruitmentConstraints",rungrp$doitall)
  if(length(pos)<2) {
    stop("No existing call to recruitmentConstraints")
    } else {
    pos <- max(pos)
    steep_l <- unlist(strsplit(rungrp$doitall[pos],split="[[:blank:]]+"))
    steep <- as.numeric(steep_l[length(steep_l)])
    steep <- steep + steep_add
    steep_l[length(steep_l)] <- steep
    rungrp$doitall[pos] <- paste(steep_l,collapse=" ")
    }
  # search the doitall for the 'recruitmentconstraints'
  # add something to whatever is in there 
  return(rungrp)
}

# ----------------------------
# 2. Growth curve        # undebug(setup.growth)
#startVB=list(c(40.437,101.747,0.34743,4.0449,0.21609),start_agepars=c(-4.1677849,-5.1871545,-4.0812922,-1.2081512))
setup.growth <- function(rungrp,VBopt) {
  a <- rungrp$doitall
  # remove all growth estimation flags from doitall except the 'use offset (173)' and offset penalty
  a <- a[grep("^[[:space:]]*1 12 1",a,invert=T)]  # est Lmin
  a <- a[grep("^[[:space:]]*1 13 1",a,invert=T)]  # est Lmax
  a <- a[grep("^[[:space:]]*1 14 1",a,invert=T)]  # est K
  a <- a[grep("^[[:space:]]*1 15 1",a,invert=T)]  # est generic sigma
  a <- a[grep("^[[:space:]]*1 16 1",a,invert=T)]  # est size-dependent sigma
  a <- a[grep("^[[:space:]]*1 184 1",a,invert=T)] # est growth deviates
  a <- a[grep("^[[:space:]]*1 226 1",a,invert=T)] # use Richards
  a <- a[grep("^[[:space:]]*1 227 1",a,invert=T)] # est Richards
  # then turn on whatever is in the option
  if(is.na(VBopt$estVB)[1]!=T) {
    for (i in 1:length(VBopt$estVB)) {
      a <- add.flag(a,1,VBopt$estVB[i],1,VBopt$estphase)
    }
  }
  rungrp$doitall <- a
  rungrp$ini$VBLmin <- c(VBopt$startVB[1],30,80)
  rungrp$ini$VBLmax <- c(VBopt$startVB[2],90,130)
  rungrp$ini$VBK <- c(VBopt$startVB[3],0.05,0.5)
  rungrp$ini$sdLatA <- c(VBopt$startVB[4],2,9)
  rungrp$ini$Ldep_sd <- c(VBopt$startVB[5],0,0.7)
  rungrp$ini$age_pars[3,] <- VBopt$start_agepars
  return(rungrp)
}

# ----------------------------
# 3. Effort creep
setup.effcreep <- function(rungrp,creeprate) {
  nfish <- length(rungrp$frq$fish$fishery)
  rungrp$frq <- effortcreep(rungrp$frq,fisheries=1:nfish,creep=creeprate)
  return(rungrp)
}

# ----------------------------
# 4. M
setup.M <- function(rungrp,newM) {
  rungrp$ini$M <- newM
  rungrp$doitall <- change.flag(rungrp$doitall,2,flagnum=33,newval=0)
  return(rungrp)
}

# ----------------------------
# 5. Startyr   rungrp<-combogrp  newstartyr <- 1965
setup.startyr <- function(rungrp,newstartyr,halfyr) {
  rungrp$frq <- start_year.frq(rungrp$frq,newstartyr,halfyr)
  return(rungrp)
}

# ----------------------------
# 6. Time split
setup.timesplit <- function(rungrp,splitx,storefish,splitset) {
  if (splitx == 1) {
    frq <- rungrp$frq ; tag <- rungrp$tag ; doitall <- rungrp$doitall
    st <- frq$fish <- storefish

    for(set in 1:length(splitset)) {
      attach(splitset[[set]])
      splitf <- st[st$nations %in% sp_nat & st$areas %in% sp_reg,3]
      lst <- timesplit.frq(frq,div.fish=splitf,sp_yrs)
      frq <- lst$frq 
      st <- frq$fish
      fishsplit <- lst$fishsplit

      tag <- timesplit.tag(tag, fishsplit)
      
      doitall <- timesplit.doitall(doitall, fishsplit)
      detach(splitset[[set]])
      }
    rungrp$frq <- frq ; rungrp$tag <- tag ; rungrp$doitall <- doitall    
    }
  return(rungrp)
  }


# ----------------------------
# 7. LF weight
setup.LFwt <- function(rungrp,newLFwt) {
  rungrp$doitall <- add.flag(rungrp$doitall,flagtype=-999,flagnum=49,newval=newLFwt,phase=7)
  return(rungrp)
}

# ----------------------------
# 7. effSD
setup.effSD <- function(rungrp,penchange) {
  if(rungrp$frq$struct$te==6) {
    m <- rungrp$frq$mat
    m[m[,7]>0,7] <- m[m[,7]>0,7] * penchange
    rungrp$frq$mat <- m
    } 
  return(rungrp)
}

# ----------------------------
# 7. LF weight
setup.LFuse <- function(rungrp,LFuse) {
  if(LFuse==0) {
    a <- rungrp$doitall
    a <- a[-grep("-999 49 500",a)]
    a <- a[-grep(" 1 11 0",a)]
    a <- a[-grep(" 1 12 0",a)[3]]
    a <- a[-grep(" 1 13 0",a)[3]]
    a <- a[-grep(" 1 14 0",a)[3]]
    a <- a[-grep(" 1 15 0",a)[3]]
    a <- a[-grep(" 1 16 0",a)[2]]
    a <- a[-grep(" 1 184 0",a)[3]]
    a <- a[-grep("-999 48 0",a)]
     rungrp$doitall <- a
    }
  return(rungrp)
}

# ----------------------------
# 8. Length-specific selectivity
setup.lensel <- function(rungrp,fisheries) {
  for (f in fisheries) {
    rungrp$doitall <- change.flag(rungrp$doitall,flagtype=-f,flagnum=26,newval=3)
    }
  return(rungrp)
}

# ----------------------------
# 9. Growth curve offsets
setup.growth.offsets <- function(rungrp,ageclasses,penwt) {
  rungrp$doitall <- add.flag(rungrp$doitall,flagtype=1,flagnum=173,newval=ageclasses)
  rungrp$doitall <- add.flag(rungrp$doitall,flagtype=1,flagnum=182,newval=penwt)
  rungrp$doitall <- add.flag(rungrp$doitall,flagtype=1,flagnum=184,newval=1)
  return(rungrp)
}

# ----------------------------
# 10. Setup q trend estimation
setup.qtrendmeth <- function(rungrp,qtrendmeth) {
  if(qtrendmeth==1) {
  q_grps <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,42,43,
  44,44,45,46,46,47,48,48,49,50,50,51,52,52,53,54,54,55,56,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,
  83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
  122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140)
  a <- rungrp$doitall
  a <- a[grep("-[[:digit:]]+ 60 ",a,invert=T)]
  for(f in 1:148) {
    a <- add.flag(a,-f,60,q_grps[f],phase=1)
    }
  varq <- c(1,2,3,4,5,7,9,11,13,15,17,19,37,38,39,40,41,44,47,50,53,56,59,62,81,83,85,87,89,90,91,92,93,94,95,96,109,111,113,115,117,119,121,123,125,127,129,131)
  for(f in 1:length(varq)) {
    a <- add.flag(a,-f,10,1,phase=1)
    }
  rungrp$doitall <- a
  }
  return(rungrp)
  }


# ----------------------------
# 10. Setup CPUEreg
setup.CPUEreg <- function(rungrp,CPUEreg) {
  regs <- switch(CPUEreg+1,c(1,2,3,4),c(1,3),c(2,4))
  stdfish <- getfishery(flag="TW",reg=c(1,2,3,4),sf1=storefish148)
  cpuefish <- getfishery(flag="TW",reg=regs,afterstartyr=1975,sf1=storefish148)
  dropfish <- stdfish[!stdfish %in% cpuefish]
  m <- rungrp$frq$mat
  m[m[,4] %in% dropfish & m[,7] != -1,7] <- m[m[,4] %in% dropfish & m[,7] != -1,7] / 100
  rungrp$frq$mat <- m
  return(rungrp)
  }

# ----------------------------
# 10. Setup earlyCPUE
setup.earlyCPUE <- function(rungrp,earlyCPUE) {
  m <- rungrp$frq$mat
  stdfish <- getfishery(flag="TW",reg=c(1,2,3,4),sf1=storefish148)
  if(earlyCPUE==1) m[m[,4] %in% stdfish & m[,7] != -1 & m[,1] < 1972,7] <- m[m[,4] %in% stdfish & m[,7] != -1 & m[,1] < 1972,7] * 100
  rungrp$frq$mat <- m
  return(rungrp)
  }


